package top.lyc829.aircontroller.ui;

import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;

import top.lyc829.aircontroller.logic.utils.CallBack;
import top.lyc829.aircontroller.logic.utils.GlobalConfig;
import top.lyc829.aircontroller.logic.utils.HttpUtils;

public class BaseActivity extends AppCompatActivity {

    private final static int DEFAULT_FLAG = -10000;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new LoadingDialog(this)
                .setLoadingText("请求中...")
                .setSuccessText("请求成功")
                .setFailedText("请求失败")
                .closeSuccessAnim()
                .closeFailedAnim()
                .setShowTime(600);
    }

    protected void doGet(final int flag, String url){
        showLoading();
        HttpUtils.create(GlobalConfig.COMMON_ADDRESS + url).doGet(new CallBack() {
            @Override
            public void returnInfo(int code,String msg, Object data) {
                handleRet(flag,code,msg,data);
            }
        });
    }

    protected void doGet(final int flag, String url, Class clazz){
        showLoading();
        HttpUtils.create(GlobalConfig.COMMON_ADDRESS + url).setRetBean(clazz).doGet(new CallBack() {
            @Override
            public void returnInfo(int code,String msg, Object data) {
                handleRet(flag,code,msg,data);
            }
        });
    }

    protected void doGet(final int flag, String url, List<String> keys){
        showLoading();
        HttpUtils.create(GlobalConfig.COMMON_ADDRESS + url).setRetBean(keys).doGet(new CallBack() {
            @Override
            public void returnInfo(int code,String msg, Object data) {
                handleRet(flag,code,msg,data);
            }
        });
    }

    protected void doGet(String url){
        doGet(DEFAULT_FLAG,url);
    }

    protected void doGet(String url,Class clazz){
        doGet(DEFAULT_FLAG,url,clazz);
    }

    protected void doGet(String url, List<String> keys){
        doGet(DEFAULT_FLAG,url,keys);
    }

    private void handleRet(int flag,int code,String msg, Object data){
        if (code == HttpUtils.SUCCESS){
            loadingDialog.setSuccessText(msg);
            showSuccess();
            if (flag == DEFAULT_FLAG){
                netCallBack(data);
            }else {
                netCallBack(flag,data);
            }
        }else {
            loadingDialog.setFailedText(msg);
            showFail();
        }
    }

    protected void netCallBack(int flag,Object data){

    }

    protected void netCallBack(Object data){

    }



    private void showLoading(){
        loadingDialog.show();
    }

    private void showSuccess(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.loadSuccess();
            }
        });
    }

    private void showFail(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.loadFailed();
            }
        });
    }

    protected void setFullscreen() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

}